home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-28 | 2.8 KB | 142 lines | [TEXT/KAHL] |
-
- // ================================================================================
- //
- // cookie.c - a fortune cookie command for the Mac
- //
- // Copyright (c) 1994 James E. O'Hearn
- //
- // You may distribute unmodified copies of this file for
- // noncommercial purposes. All other rights are reserved.
- //
- // ================================================================================
-
- // Constants
-
- #define BASE_STRING 16000
-
- // CodeWarrior requires an A4 setup
-
- #ifdef __MWERKS__
- #include <A4Stuff.h>
- #endif
-
- // Read standard nShell includes
-
- #include "nshc.h"
- #include "nshc_utl.proto.h"
- #include "str_utl.proto.h"
-
- // Prototypes
-
- short OpenMyResources (t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls);
- OSErr DoCookie (t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls);
-
- // ================================================================================
-
- void main (t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
- {
-
- OSErr error;
-
-
- #ifdef __MWERKS__
- long oldA4 = SetCurrentA4();
- #endif
-
- if (!nshc_bad_version (nshc_parms, nshc_calls, NSHC_VERSION))
- {
-
- // otherwise, handle requests from the application
-
- switch (nshc_parms->action)
- {
- case nsh_start:
- error = DoCookie (nshc_parms, nshc_calls);
- break;
- case nsh_continue:
- case nsh_idle:
- case nsh_stop:
- error = NSHC_NO_ERR;
- break;
- }
-
- // tell the application we are done
-
- nshc_parms->action = nsh_idle;
- nshc_parms->result = error;
-
- }
-
- #ifdef __MWERKS__
- SetA4(oldA4); // CodeWarrior needs to restore A4
- #endif
- }
-
- // ================================================================================
-
- OSErr DoCookie (t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
- {
-
- short numStrings,theString,fileRef;
- StringHandle string;
-
-
- fileRef = OpenMyResources (nshc_parms, nshc_calls);
-
- if (fileRef < 0)
- {
- nshc_calls->NSH_putStr_err ("\pcookie: Could not open resource file.\r");
- return (NSHC_ERR_GENERAL);
- }
-
- numStrings = Count1Resources ('STR ');
-
- if (!numStrings)
- {
- nshc_calls->NSH_putStr_err ("\pcookie: no string resources found.\r");
- return (NSHC_ERR_GENERAL);
- }
-
- theString = BASE_STRING + (TickCount() % numStrings);
-
- string = GetString (theString);
-
- if (!string)
- {
- nshc_calls->NSH_putStr_err ("\pcookie: string could not be loaded.\r");
- return (NSHC_ERR_GENERAL);
- }
-
- HLock (string);
- nshc_calls->NSH_putStr (*string);
- HUnlock (string);
-
- ReleaseResource (string);
- CloseResFile (fileRef);
-
- return (NSHC_NO_ERR);
-
- }
-
- // ================================================================================
-
- short OpenMyResources (t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
- {
-
- Str255 fileName;
- short fileRef;
-
-
- pStrFromC (fileName, &nshc_parms->arg_buf[nshc_parms->argv[0]]);
-
- fileRef = -1;
-
- if ( !nshc_calls->NSH_path_which (fileName))
- {
- fileRef = OpenResFile (fileName);
- }
-
- return (fileRef);
- }
-
-